package bb.lanxing.model.json;

import android.os.Parcel;
import android.os.Parcelable;

import com.alibaba.fastjson.annotation.JSONField;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;

import bb.lanxing.util.JsonUtil;
import bb.lanxing.utils.DateUtil;


public class Topic implements Serializable, Parcelable {
    public static final Creator<Topic> CREATOR = new Creator<>() {
        @Override
        public Topic createFromParcel(Parcel parcel) {
            return new Topic(parcel);
        }

        @Override
        public Topic[] newArray(int i) {
            return new Topic[i];
        }
    };
    private static final long serialVersionUID = 1;
    private Channel channel;
    @JSONField(name = "channel_id")
    private int channelId;
    @JSONField(name = "comment_count")
    private int commentCount;
    private String content;
    @JSONField(format = "yyyy-MM-dd'T'HH:mm:ss", name = "create_time")
    private long createTime;
    @JSONField(name = "top")
    private boolean isTop;
    private double latitude;
    private double longitude;
    @JSONField(name = "pic_url")
    private String photoUrl;
    @JSONField(name = "id")
    private long serverId;
    @JSONField(name = "user")
    private ServerUser serverUser;
    private String title;
    @JSONField(name = "t_state")
    private int topicState;
    @JSONField(format = "yyyy-MM-dd'T'HH:mm:ss", name = "update_time")
    private long updateTime;
    @JSONField(name = "view_count")
    private int viewCount;

    @Override 
    public int describeContents() {
        return 0;
    }

    public Topic() {
        this.topicState = -1;
    }

    public Topic(JSONObject jSONObject) throws JSONException {
        this.topicState = -1;
        setServerId(JsonUtil.getIntegerValue("id", jSONObject));
        setChannelId(JsonUtil.getIntegerValue("channel_id", jSONObject));
        setTitle(JsonUtil.getStringValue("title", jSONObject));
        setContent(JsonUtil.getStringValue("content", jSONObject));
        setLatitude(JsonUtil.getDoubleValue("latitude", jSONObject));
        setLongitude(JsonUtil.getDoubleValue("longitude", jSONObject));
        setCreateTime(JsonUtil.getStringValue("create_time", jSONObject));
        setUpdateTime(JsonUtil.getStringValue("update_time", jSONObject));
        setViewCount(JsonUtil.getIntegerValue("view_count", jSONObject));
        setCommentCount(JsonUtil.getIntegerValue("comment_count", jSONObject));
        setTop(JsonUtil.getBooleanValue("top", jSONObject));
        if (jSONObject.has("t_state")) {
            setTopicState(JsonUtil.getIntegerValue("t_state", jSONObject));
        }
        setPhotoUrl(JsonUtil.getStringValue("pic_url", jSONObject));
        JSONObject objectValue = JsonUtil.getObjectValue("user", jSONObject);
        if (objectValue != null) {
            setServerUser(new ServerUser(objectValue));
        }
        JSONObject objectValue2 = JsonUtil.getObjectValue("channel", jSONObject);
        if (objectValue2 != null) {
            setChannel(new Channel(objectValue2));
        }
    }

    public long getServerId() {
        return this.serverId;
    }

    public void setServerId(long j) {
        this.serverId = j;
    }

    public ServerUser getServerUser() {
        return this.serverUser;
    }

    public void setServerUser(ServerUser serverUser) {
        this.serverUser = serverUser;
    }

    public Channel getChannel() {
        return this.channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public String getContent() {
        return this.content;
    }

    public void setContent(String str) {
        this.content = str;
    }

    public double getLatitude() {
        return this.latitude;
    }

    public void setLatitude(double d) {
        this.latitude = d;
    }

    public double getLongitude() {
        return this.longitude;
    }

    public void setLongitude(double d) {
        this.longitude = d;
    }

    public int getViewCount() {
        return this.viewCount;
    }

    public void setViewCount(int i) {
        this.viewCount = i;
    }

    public int getTopicState() {
        return this.topicState;
    }

    public void setTopicState(int i) {
        this.topicState = i;
    }

    public int getCommentCount() {
        return this.commentCount;
    }

    public void setCommentCount(int i) {
        this.commentCount = i;
    }

    public long getCreateTime() {
        return this.createTime;
    }

    public void setCreateTime(String str) {
        if (str != null) {
            this.createTime = DateUtil.parse(str, 3);
        }
    }

    public long getUpdateTime() {
        return this.updateTime;
    }

    public void setUpdateTime(long j) {
        this.updateTime = j;
    }

    public void setUpdateTime(String str) {
        if (str != null) {
            this.updateTime = DateUtil.parse(str, 3);
        }
    }

    public String getPhotoUrl() {
        return this.photoUrl;
    }

    public void setPhotoUrl(String str) {
        this.photoUrl = str;
    }

    public boolean isTop() {
        return this.isTop;
    }

    public void setTop(boolean z) {
        this.isTop = z;
    }

    public String getContentWithTop() {
        StringBuilder sb = new StringBuilder();
        sb.append(isTop() ? "【置顶】" : "");
        sb.append(getContent());
        return sb.toString();
    }

    public long getChannelId() {
        return this.channelId;
    }

    public void setChannelId(int i) {
        this.channelId = i;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String str) {
        this.title = str;
    }

    @Override 
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeLong(this.serverId);
        parcel.writeInt(this.channelId);
        parcel.writeParcelable(this.serverUser, i);
        parcel.writeParcelable(this.channel, i);
        parcel.writeString(this.title);
        parcel.writeString(this.content);
        parcel.writeDouble(this.latitude);
        parcel.writeDouble(this.longitude);
        parcel.writeInt(this.viewCount);
        parcel.writeInt(this.commentCount);
        parcel.writeLong(this.createTime);
        parcel.writeLong(this.updateTime);
        parcel.writeByte(this.isTop ? (byte) 1 : (byte) 0);
        parcel.writeString(this.photoUrl);
        parcel.writeInt(this.topicState);
    }

    protected Topic(Parcel parcel) {
        this.topicState = -1;
        this.serverId = parcel.readLong();
        this.channelId = parcel.readInt();
        this.serverUser = (ServerUser) parcel.readParcelable(Topic.class.getClassLoader());
        this.channel = (Channel) parcel.readParcelable(Topic.class.getClassLoader());
        this.title = parcel.readString();
        this.content = parcel.readString();
        this.latitude = parcel.readDouble();
        this.longitude = parcel.readDouble();
        this.viewCount = parcel.readInt();
        this.commentCount = parcel.readInt();
        this.createTime = parcel.readLong();
        this.updateTime = parcel.readLong();
        this.isTop = parcel.readByte() == 1;
        this.photoUrl = parcel.readString();
        this.topicState = parcel.readInt();
    }
}
